package com.example.com.common.app;

import android.support.annotation.IntDef;
import android.support.v7.util.DiffUtil;

import com.example.com.common.view.recycler.RecyclerAdapter;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

/**
 * 对RecyclerView进行的一个简单的Presenter封装
 * Created by ChenYiYao on 2018/2/8.
 */

public class BaseRecyclerPresenter<View extends BaseContract.BaseRecyclerView>
        extends BasePresenter<View> {


    public BaseRecyclerPresenter(View view) {
        super(view);
    }

    /**
     * 普通的刷新列表数据
     *
     * @param list 新数据
     */
    protected void RefreshData(final List list) {
        if (mView == null) {
            return;
        }
        RecyclerAdapter adapter = mView.getRecyclerViewAdapter();
        adapter.replace(list);
        mView.onAdapterDataChanged();
    }


    /**
     * Diff工具刷新界面操作
     *
     * @param diffResult 一个差异的结果集
     * @param dataList   具体的新数据
     */
    protected void DiffRefreshData(final DiffUtil.DiffResult diffResult, final List dataList) {
        if (mView == null) {
            return;
        }
        RecyclerAdapter adapter = mView.getRecyclerViewAdapter();
        // 改变数据集合并不通知界面刷新
        adapter.getItems().clear();
        adapter.getItems().addAll(dataList);
        //进行增量更新
        diffResult.dispatchUpdatesTo(adapter);
        //通知界面刷新
        mView.onAdapterDataChanged();
    }


    interface WayConstants {
        int NORMAL = 1; //普通方式
        int DIFF = 2; //DiffUtils刷新

        @IntDef({NORMAL, DIFF})
        @Retention(RetentionPolicy.SOURCE)
        public @interface Unit {
        }
    }
}

